package my.engine.pathHandlers;

import my.engine.core.AbstractPathHandler;
import my.engine.core.AdminUserFilter;
import my.engine.core.MessageFilter;
import my.engine.objects.PathNode;
import my.engine.objects.RequestBean;
import my.engine.objects.entities.Property;
import my.engine.utils.SettingsUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: McMan
 * Date: 14.03.2008
 * Time: 9:13:07
 */
public class SettingHandler extends AbstractPathHandler {

    public void processRequest(HttpServletRequest request, HttpServletResponse response, RequestBean requestBean) {
        if (AdminUserFilter.isUserAdmin()) {
            if (request.getParameter("action") != null && request.getParameter("action").equals("saveProps")) {
                Map<String, String[]> requestParams = (Map<String, String[]>) request.getParameterMap();

                Map<String, String> params = new HashMap<String, String>();
                for (String key : requestParams.keySet()) {
                    String value = requestParams.get(key)[0];
                    if (!key.equals("submit") && !key.equals("action")) {
                        params.put(key, value);
                    }
                }

                SettingsUtil.setPortalProperties(params);

                MessageFilter.addMessage("Properties successfully saved");
            } else {
                List<Property> props = SettingsUtil.getPortalPropsAsObjects();
                request.setAttribute("portalProperties", props);
            }
        } else {
            MessageFilter.addError("Please use login form");
        }
    }

    public boolean continuePocess() {
        return true;
    }

    public List<PathNode> getResourcePath() {
        return null;
    }
}
